//
//  MH2ViewController.m
//  Food2ForkTestNhom3
//
//  Created by HOCVIEN on 4/21/15.
//  Copyright (c) 2015 LamQuoc. All rights reserved.
//

#import "MH2ViewController.h"

@interface MH2ViewController ()
@property (nonatomic, strong) NSDictionary* photos2;

@end

@implementation MH2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUI];
}

- (void)loadUI {
    [self.myActivity startAnimating];
    [self getRecipeID:^(NSDictionary *myDic, NSError *error) {
        
        self.photos2 = [[NSDictionary alloc]initWithDictionary:myDic];
        
        NSArray* myArray = [self.photos2 objectForKey:@"ingredients"];
        self.navigationItem.title = [self.photos2 objectForKey:@"title"];
        NSString* chuoi =[[NSString alloc]init];
        for (NSString* ct in myArray) {
            chuoi = [chuoi stringByAppendingString:[NSString stringWithFormat:@"%@\r",ct]];
        }
       
        UIFont *customFont = [UIFont fontWithName:@"AmericanTypewriter" size:15];
        self.myTextfield.text = chuoi;
        self.myTextfield.editable=NO;
        self.myTextfield.backgroundColor = [UIColor clearColor];
        self.myTextfield.font = customFont;
        
        UIFont *customFont2 = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:17];
        self.titelMH2.font=customFont2;
        
        NSString* urlString = [self.photos2 objectForKey:@"image_url"];
        
        dispatch_async(dispatch_queue_create("MYIMAGE", nil), ^{
            NSURL *url=[[NSURL alloc] initWithString:urlString];
            NSData* myData = [[NSData alloc]initWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.imageMH2.image = [UIImage imageWithData:myData];
                self.myBackground.image = [UIImage imageWithData:myData];
                UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
              
                UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
                effectView.frame = self.view.frame;
              
                [self.myBackground addSubview:effectView];
                [self.myActivity stopAnimating];
            });
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationItem.title = [self.photos2 objectForKey:@"title"];
    self.navigationController.navigationBar.topItem.title=@"Recipes";
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 

}


-(void)getRecipeID:(void(^)(NSDictionary* myDic,NSError* error))callback{

    
    if (nil != self.myIDrecipes) {
        NSString* myString = [NSString stringWithFormat:@"http://food2fork.com/api/get?key=869e3bea04b46348efa5db95818f9994&rId=%@",self.myIDrecipes];
        NSURL* url = [[NSURL alloc]initWithString:myString];
        
        NSURLRequest* request = [[NSURLRequest alloc]initWithURL:url];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (connectionError==nil) {
                NSError* myError;
                NSDictionary* myDic =[NSJSONSerialization JSONObjectWithData:data options:0 error:&myError];
                NSDictionary* requestDic = [myDic objectForKey:@"recipe"];
                
                if (callback) {
                    callback(requestDic,myError);
                }
                
            }
        }];
    } else {
        NSLog(@"Error: myID is nil..!!!");
    }
}


-(NSDictionary *)getNextFood:(NSDictionary *)curentFood{
    
    NSString* currentID = [curentFood objectForKey:@"recipe_id"];
    NSDictionary* dictionary;
    for (int i = 0; i < self.idArray.count-1; i++) {
        dictionary = [self.idArray objectAtIndex:i];
        if ([[dictionary objectForKey:@"recipe_id"] isEqualToString:currentID]) {
            return [self.idArray objectAtIndex:i+1];
        }
    }
    
    return self.idArray[0];
}

-(NSDictionary *)getPrevFood:(NSDictionary *)curentFood{
    
    NSString* currentID = [curentFood objectForKey:@"recipe_id"];
    NSDictionary* dictionary;
    int k = 0;
    for (int i = 0; i < self.idArray.count; i++) {
        dictionary = [self.idArray objectAtIndex:i];
        if ([[dictionary objectForKey:@"recipe_id"] isEqualToString:currentID]) {
            k = i-1;
            if (k < 0) {
                k = self.idArray.count -1;
            } else {
                k = i-1;
            }
            
            return [self.idArray objectAtIndex:k];
        }
    }
    
    return self.idArray[0];
}

- (IBAction)ibaNext:(id)sender {
    NSDictionary *dictionary = [self getNextFood:self.photos2];
    self.myIDrecipes = [dictionary objectForKey:@"recipe_id"];
    NSLog(@"myIDrecipes: %@", self.myIDrecipes);
    //NSLog(@"Next>>>:%@<<<<",dictionary);
    [self loadUI];
}

- (IBAction)ibaPrev:(id)sender {
    NSDictionary *dictionary = [self getPrevFood:self.photos2];
    self.myIDrecipes = [dictionary objectForKey:@"recipe_id"];
    NSLog(@"myIDrecipes: %@", self.myIDrecipes);
    //NSLog(@"Next>>>:%@<<<<",dictionary);
    
    [self loadUI];

}
@end
